<?php


namespace api\zwt\controller;


use api\zwt\model\UserModel;
use api\zwt\validate\UserValidate;
use api\zwt\controller\UserController;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
use think\Request;

class WalletController extends RestBaseController
{
    //充值金额
    public function money(Request $request)
    {
        //获取金额
        $money = input('money/f');
        if(!$money)
        {
            $this->error('请输入金额');
        }
        //判断用户
        $user = UserModel::find($request->user_id);
        if(!$user)
        {
            $this->error('参数错误');
        }
        $after = $user['wallet'] + $money;
        // 启动事务
        Db::startTrans();
        //作用域
        $addLog = false;
        $addMoney = false;

        try{
            //记录流水
            $addLog = add_money_log($request->user_id,$money,'余额充值',$user['wallet'],$after,1);
            //增加账户余额
            $addMoney = UserModel::where('id',$request->user_id)->inc('wallet',$money)->update();
        }catch (\Exception $exception){
            Db::rollback();
            $this->error('充值失败11');
        }
        if ($addMoney && $addLog){
            Db::commit();
            $this->success('充值成功');
        }else{
            Db::rollback();
            $this->error('充值失败');
        }

    }
}